package com.sharemarking.system.dto;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.sharemarking.system.entity.User;

/**
 * TODO 序列化与反序列化未实测
 * @author song
 *
 */
public class UserDetailImpl implements UserDetails {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String username;
	private String password;
	private String name;
	private boolean locked;
	private boolean enabled;
	private Set<Integer> roles;
	private Set<Integer> permissions;
	
	public UserDetailImpl(User user,List<Integer> roleList,List<Integer> permissionIdList,boolean locked) {
		this.id=user.getId();
		this.username=user.getLoginName();
		this.password=user.getPassword();
		this.enabled=Boolean.TRUE.equals(user.getEnabled());
		this.roles=new HashSet<Integer>();
		this.permissions=new HashSet<Integer>();
		for(Integer role:roleList)this.roles.add(role);
		for(Integer id:permissionIdList)this.permissions.add(id);
		name=user.getName();
		this.locked=locked;
	}

	@Override
	public List<SimpleGrantedAuthority> getAuthorities() {
		return roles.stream().map(s->new SimpleGrantedAuthority("ROLE_"+s)).collect(Collectors.toList());
		//return list;
	}
	
	public Integer getId(){
		return id;
	}
	
	public String getName(){
		return name;
	}
	
	public Set<Integer> getPermissions(){
		return permissions;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return !locked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}
	
//	private void writeObject(ObjectOutputStream out) throws IOException {
//		out.writeInt(id);
//		byte[] usernameBytes=username.getBytes("utf-8");
//		out.writeShort(usernameBytes.length);
//		out.write(usernameBytes);
//		out.writeShort(password.length());
//		out.writeBytes(password);
//		byte[] nameBytes=name.getBytes("utf-8");
//		out.writeShort(nameBytes.length);
//		out.write(nameBytes);
//		
//		out.writeBoolean(enabled);
//		out.writeShort(roles.size());
//		for(Integer s:roles)out.writeInt(s);
//		out.writeShort(permissions.size());
//		for(Integer p:permissions)out.writeInt(p);
//		out.flush();
//	}
//	
//	private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
//		id=in.readInt();
//		byte[] tempBytes=new byte[1024];
//		short length=in.readShort();
//		in.read(tempBytes,0,length);
//		username=new String(tempBytes,0,length,"utf-8");
//		length=in.readShort();
//		in.read(tempBytes,0,length);
//		password=new String(tempBytes,0,length);
//		length=in.readShort();
//		in.read(tempBytes,0,length);
//		name=new String(tempBytes,0,length,"utf-8");
//		enabled=in.readBoolean();
//		length=in.readShort();
//		roles=new HashSet<Integer>(length);
//		for(int i=0;i<length;i++)roles.add(in.readInt());
//		length=in.readShort();
//		permissions=new HashSet<Integer>(length);
//		for(int i=0;i<length;i++)permissions.add(in.readInt());
//	}
	
}
